const express = require("express");
const Users = require("../models/login");
const jwt = require("jsonwebtoken");
const cookieParser = require('cookie-parser')
const secret = 'laowang';
const router = new express.Router();
router.use(cookieParser());
router.post("/reg", async(req, res) => {
    const { username, password } = req.body;
    const username_reg = /^[A-z0-9]{3,12}$/;
    const password_reg = /^[A-z0-9]{3,12}$/;
    if (username_reg.test(username.trim()) && password_reg.test(password.trim())) {
        const result_find = await Users.findOne({
            username
        });
        console.log(result_find);
        if (result_find) {
            res.setHeader("Access-Control-Allow-Origin", "*");
            res.setHeader("Access-Control-Allow-Headers", "*");
            res.setHeader("Access-Control-Allow-Methods", "*");
            res.send('用户已存在');
        } else {
            const result = await Users.create({
                username,
                password
            });
            res.setHeader("Access-Control-Allow-Origin", "*");
            res.setHeader("Access-Control-Allow-Headers", "*");
            res.setHeader("Access-Control-Allow-Methods", "*");
            res.send('注册成功');
        }
    } else {
        res.setHeader("Access-Control-Allow-Origin", "*");
        res.setHeader("Access-Control-Allow-Headers", "*");
        res.setHeader("Access-Control-Allow-Methods", "*");
        res.send("注册失败")
    }
})
router.post("/login", async(req, res) => {
    const { username, password } = req.body;
    const rea = await Users.findOne({ username });
    if (rea) {
        if (rea.username == username && rea.password == password) {
            jwt.sign(req.body, secret, {
                expiresIn: '1h'
            }, (err, token) => {
                if (!err) {
                    console.log(token);
                    res.cookie('token', token, {
                        maxAge: 1000 * 60 * 60
                    })
                    res.setHeader("Access-Control-Allow-Origin", "*");
                    res.setHeader("Access-Control-Allow-Headers", "*");
                    res.setHeader("Access-Control-Allow-Methods", "*");
                    res.send('登录成功！！');
                } else {
                    console.log(err);
                    res.setHeader("Access-Control-Allow-Origin", "*");
                    res.setHeader("Access-Control-Allow-Headers", "*");
                    res.setHeader("Access-Control-Allow-Methods", "*");
                    res.send('用户名密码错误');
                }
            })
        } else {
            res.setHeader("Access-Control-Allow-Origin", "*");
            res.setHeader("Access-Control-Allow-Headers", "*");
            res.setHeader("Access-Control-Allow-Methods", "*");
            res.send("用户名密码错误")
        }
    } else {
        res.setHeader("Access-Control-Allow-Origin", "*");
        res.setHeader("Access-Control-Allow-Headers", "*");
        res.setHeader("Access-Control-Allow-Methods", "*");
        res.send("用户名密码错误")
    }
})
router.get('/mianlogin', (req, res) => {
    console.log(req.cookies);
    jwt.verify(req.cookies.token, secret, (err, data) => {
        if (!err) {
            console.log(data);
            res.setHeader("Access-Control-Allow-Origin", "*");
            res.setHeader("Access-Control-Allow-Headers", "*");
            res.setHeader("Access-Control-Allow-Methods", "*");
            res.send('登录成功');
        } else {
            console.log(err);
            res.setHeader("Access-Control-Allow-Origin", "*");
            res.setHeader("Access-Control-Allow-Headers", "*");
            res.setHeader("Access-Control-Allow-Methods", "*");
            res.send('请重新登录');
        }
    })
})
module.exports = router;